#!/usr/bin/ python
# -*- coding: utf-8 -*-
# @Time    : 2021-05-18 10:06
# @Author  : ysl
# @File    : shezai.py
# @Software: PyCharm
import time

import requests
import json


class SmlinesGoodTracking():

    def __str__(self):
        print('https://esvc.smlines.com/smline/CUP_HOM_3301.do?sessLocale=zh')

    def __init__(self):
        self.headers = {
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, "
                          "like Gecko) Chrome/90.0.4430.212 Safari/537.36",
        }

    def get_order(self,craw_id):
        mow_millisecond = int(round(time.time() * 1000))
        """
        获取订单信息
        :return:
        """
        url = 'http://esvc.smlines.com/smline/CUP_HOM_3301GS.do?_search=false&nd='+str(mow_millisecond)+'&rows=10000&page=1&sidx=&sord=asc&f_cmd=121&search_type=B&search_name='+str(craw_id)+'&cust_cd=&_='+str(mow_millisecond)
        resp = requests.get(url, headers=self.headers).json()
        datas = resp.get('list')
        containers = []
        for data in datas:
            cop_no = data.get('copNo')
            bl_no = data.get('blNo')
            cntr_no = data.get('cntrNo')
            seal_no = data.get('sealNo')
            date_time = data.get('eventDt')
            place = data.get('yardNm')
            status = data.get('statusNm')
            size = data.get('cntrTpszNm')
            weight = data.get('weight')
            items = {
                "cop_no": cop_no,
                "bl_no": bl_no,
                "cntr_no": cntr_no,
                "seal_no":seal_no,
                "date_time": date_time,
                "place": place,
                "status": status,
                "size": size,
                "weight": weight,
            }
            containers.append(items)
        return containers

    def shipment_information(self, code):
        # 开航信息
        url = "http://esvc.smlines.com/smline/CUP_HOM_3301GS.do"
        data = {
            "f_cmd": 124,
            "bkg_no": code
        }
        resp = requests.post(url, data=data, headers=self.headers).json()
        data = resp.get('list')
        data = data[0] if data else dict()
        result = {}
        result["shipment_information"] = data
        return  result
        port_of_loading = data.get('polNm')
        departure_date = data.get('etd')
        arrival_time = data.get('eta')
        port_of_discharging = data.get('podNm')
        print('开航信息====  ',port_of_loading, departure_date, arrival_time, port_of_discharging)

    def get_date(self,cntr_no,bkg_no,cop_no):
        # 获取物流路径信息
        url = "http://esvc.smlines.com/smline/CUP_HOM_3301GS.do"
        # data = {
        #     "f_cmd": 128,
        #     "cntr_no": "CCLU2974053",
        #     "bkg_no": "XMN0A2020600",
        #     "cop_no": "CXMN0814090613",
        # }
        data = {
            "f_cmd": 128,
            "cntr_no": str(cntr_no),
            "bkg_no": str(bkg_no),
            "cop_no": str(cop_no),
        }
        resp = requests.post(url, data=data, headers=self.headers).json()
        data = resp.get('list')
        print('当前物流路径信息',data)
        data = data[0] if data else dict()
        first_nod = data.get('firstNod')
        last_nod = data.get('lastNod')
        print('getOrder === ', first_nod)
        print(first_nod, last_nod)
        return [first_nod,last_nod]

    def get_goods_detail(self, code,cntr_no,cop_no):
        print('获取货物追踪明细')
        """
        获取货物追踪明细
        :return:
        """
        url = "http://esvc.smlines.com/smline/CUP_HOM_3301GS.do"
        # data = {
        #     "f_cmd": 125,
        #     "cntr_no": "CCLU2974053",
        #     "bkg_no": code,
        #     "cop_no": "CXMN0814090613"
        # }
        data = {
            "f_cmd": 125,
            "cntr_no": str(cntr_no),
            "bkg_no": code,
            "cop_no": str(cop_no)
        }
        resp = requests.post(url, data=data, headers=self.headers).json()
        datas = resp.get('list')
        result = {}
        result['goods_detail'] = datas

        return  result
        # for data in datas:
        #     status = data.get('statusNm')
        #     location = data.get('placeNm')
        #     locations = data.get('yardNm')
        #     event_date = data.get('eventDt')
        #     print('当前内容')
        #     print(f'{status}, {location}, {locations}, {event_date}')

    def run(self,craw_id):
        containers = self.get_order(craw_id)
        for item in containers:
            code = item.get('bl_no')
            cntr_no = item.get('cntr_no')
            bkg_no = item.get('bkg_no')
            cop_no = item.get('cop_no')
            item.update(self.shipment_information(code))
            item.update(self.get_goods_detail(code,cntr_no,cop_no))
            # self.get_date(cntr_no,bkg_no,cop_no)
        return  containers

#
if __name__ == '__main__':
    g = SmlinesGoodTracking()
    result = g.run("XMN1A0699600")
    from pprint import pprint
    pprint(result)

# if __name__ == '__main__':
#     "XMN0A2020600单个箱号，  XMN1A0699600"